package com.xiaoluo.lottery.web;

import com.xiaoluo.lottery.common.asyn.Monitor;
import com.xiaoluo.lottery.common.utils.ApplicationContextUtil;
import com.xiaoluo.lottery.common.weixin.utils.Configure;
import com.xiaoluo.lottery.service.WeiXinServiceImpl;
import com.xl.property.PropertyEtcdConfigurer;
import com.xl.tool.redis.JedisResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import java.util.Properties;

/**
 * Created by Caedmon on 2015/8/17.
 */
public class InitializerServlet extends DispatcherServlet {
    private Logger logger = LoggerFactory.getLogger(InitializerServlet.class);

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
        ApplicationContextUtil.context = context;

        PropertyEtcdConfigurer configurer = (PropertyEtcdConfigurer)context.getBean("propertyConfigurer");
        ApplicationContextUtil.weixinService = (WeiXinServiceImpl)context.getBean("weiXinServiceImpl");

        Properties sysProperties = configurer.getProperties();
        JedisResource.getInstance().init(sysProperties);
        Configure.initialize(sysProperties);
        //
        new Thread(new Monitor()).start();
    }

}
